<template>
  <el-dialog v-model="visible" :title="!dataForm.tskId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="联络任务名字" prop="tskName">
        <el-input v-model="dataForm.tskName" placeholder="联络任务名字"></el-input>
      </el-form-item>
      <el-form-item label="联络任务标题" prop="tskTitle">
        <el-input v-model="dataForm.tskTitle" placeholder="联络任务标题"></el-input>
      </el-form-item>
      <el-form-item label="展会id" prop="exhId">
        <el-input v-model="dataForm.exhId" placeholder="展会id"></el-input>
      </el-form-item>
      <el-form-item label="联络任务类型" prop="tskType">
        <el-input v-model="dataForm.tskType" placeholder="联络任务类型"></el-input>
      </el-form-item>
      <el-form-item label="temp_path" prop="tskTempPath">
        <el-input v-model="dataForm.tskTempPath" placeholder="temp_path"></el-input>
      </el-form-item>
      <el-form-item label="grade" prop="tskGrade">
        <el-input v-model="dataForm.tskGrade" placeholder="grade"></el-input>
      </el-form-item>
      <el-form-item label="bdate" prop="tskBDate">
        <el-input v-model="dataForm.tskBDate" placeholder="bdate"></el-input>
      </el-form-item>
      <el-form-item label="creatorId" prop="tskCreatorId">
        <el-input v-model="dataForm.tskCreatorId" placeholder="creatorId"></el-input>
      </el-form-item>
      <el-form-item label="modifierId" prop="tskModifierId">
        <el-input v-model="dataForm.tskModifierId" placeholder="modifierId"></el-input>
      </el-form-item>
      <el-form-item label="modifyTime" prop="tskModifyTime">
        <el-input v-model="dataForm.tskModifyTime" placeholder="modifyTime"></el-input>
      </el-form-item>
      <el-form-item label="stufs" prop="tskStufs">
        <el-input v-model="dataForm.tskStufs" placeholder="stufs"></el-input>
      </el-form-item>
      <el-form-item label="qtrId" prop="qtrId">
        <el-input v-model="dataForm.qtrId" placeholder="qtrId"></el-input>
      </el-form-item>
      <el-form-item label="是否存在替换" prop="tskIfShare">
        <el-input v-model="dataForm.tskIfShare" placeholder="是否存在替换"></el-input>
      </el-form-item>
      <el-form-item label="source" prop="tskSource">
        <el-input v-model="dataForm.tskSource" placeholder="source"></el-input>
      </el-form-item>
      <el-form-item label="syState" prop="tskSysState">
        <el-input v-model="dataForm.tskSysState" placeholder="syState"></el-input>
      </el-form-item>
      <el-form-item label="remark" prop="tskRemark">
        <el-input v-model="dataForm.tskRemark" placeholder="remark"></el-input>
      </el-form-item>
      <el-form-item label="flag" prop="tskFlag">
        <el-input v-model="dataForm.tskFlag" placeholder="flag"></el-input>
      </el-form-item>
      <el-form-item label="rxMail" prop="tskRxMail">
        <el-input v-model="dataForm.tskRxMail" placeholder="rxMail"></el-input>
      </el-form-item>
      <el-form-item label="wizardFile" prop="tskWizardFile">
        <el-input v-model="dataForm.tskWizardFile" placeholder="wizardFile"></el-input>
      </el-form-item>
      <el-form-item label="mailSernder" prop="tskMailSender">
        <el-input v-model="dataForm.tskMailSender" placeholder="mailSernder"></el-input>
      </el-form-item>
      <el-form-item label="ojaId" prop="ojaId">
        <el-input v-model="dataForm.ojaId" placeholder="ojaId"></el-input>
      </el-form-item>
      <el-form-item label="ifAllowSkip" prop="tskIfAllowSkip">
        <el-input v-model="dataForm.tskIfAllowSkip" placeholder="ifAllowSkip"></el-input>
      </el-form-item>
      <el-form-item label="ifConToPdf" prop="tskIfConToPdf">
        <el-input v-model="dataForm.tskIfConToPdf" placeholder="ifConToPdf"></el-input>
      </el-form-item>
      <el-form-item label="annexFile" prop="tskAnnexFile">
        <el-input v-model="dataForm.tskAnnexFile" placeholder="annexFile"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  tskId: "",
  tskName: "",
  tskTitle: "",
  exhId: "",
  tskType: "",
  tskTempPath: "",
  tskGrade: "",
  tskBDate: "",
  tskCreatorId: "",
  tskModifierId: "",
  tskCreateTime: "",
  tskModifyTime: "",
  tskStufs: "",
  qtrId: "",
  tskIfShare: "",
  tskSource: "",
  tskSysState: "",
  tskRemark: "",
  tskFlag: "",
  tskRxMail: "",
  tskWizardFile: "",
  tskMailSender: "",
  ojaId: "",
  tskIfAllowSkip: "",
  tskIfConToPdf: "",
  tskAnnexFile: "",
  creator: "",
  updater: "",
  tskUpdateTime: "",
});

const rules = ref({
  tskName: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  tskTitle: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

const init = (tskId?: number) => {
  visible.value = true;
  dataForm.tskId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (tskId) {
    getInfo(tskId);
  }
};

// 获取信息
const getInfo = (tskId: number) => {
  baseService.get("/crm/crmtask/" + tskId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.tskId ? baseService.post : baseService.put)("/crm/crmtask", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>